/********************************************************************************
    Author :
********************************************************************************/

/********************************************************************************
    Include Files :
********************************************************************************/
#include "extern.h"
    #include "sdk_project_config.h"
#include "alloc.h"
	#include "Adc.h"
/********************************************************************************
    Defination :
********************************************************************************/

/********************************************************************************
    Comment : The configuration of ADC
********************************************************************************/
#define ADC_INSTANCE    0UL

#define ADC_VREFH       5.0f
#define ADC_VREFL       0.0f

#define ADC_CHANNEL_INDEX2 ADC_0_ChnConfig2
#define ADC_CHANNEL_INDEX3 ADC_0_ChnConfig3

/********************************************************************************
    Type name :
********************************************************************************/

/********************************************************************************
    Comment : The description of type name
********************************************************************************/

/********************************************************************************
    Private variables :
********************************************************************************/

/********************************************************************************
    Comment : The range of adc
********************************************************************************/
PRIVATE uint16 u16AdcMax = 0;
/********************************************************************************
    Private function declaration :
********************************************************************************/

/********************************************************************************
    Public function :
********************************************************************************/

/********************************************************************************
    Description :
        The initial function of ADC
********************************************************************************/
PUBLIC FUNC(void,ADC_CODE)Adc_vidInitFunction(void)
{
    if (ADC_0_ConvConfig0.resolution == ADC_RESOLUTION_8BIT)
    {
        u16AdcMax = (uint16) (1 << 8);
    }
	else if (ADC_0_ConvConfig0.resolution == ADC_RESOLUTION_10BIT)
    {
        u16AdcMax = (uint16) (1 << 10);
    }
	else
    {
        u16AdcMax = (uint16) (1 << 12);
    }

	ADC_DRV_ConfigConverter(ADC_INSTANCE, &ADC_0_ConvConfig0);
	ADC_DRV_AutoCalibration(ADC_INSTANCE);
}
/********************************************************************************
    Description :
        Get Adc channel2 value
********************************************************************************/
PUBLIC FUNC(float,ADC_CODE)Adc_ftGetChannel2Value(void)
{
    uint16 u16AdcRawValue;
    float ftAdcValue;
    /* Configure ADC channel and software trigger a conversion */
    ADC_DRV_ConfigChan(ADC_INSTANCE, 0U, &ADC_CHANNEL_INDEX2);
    /* Wait for the conversion to be done */
    ADC_DRV_WaitConvDone(ADC_INSTANCE);
    /* Store the channel result into a local variable */
    ADC_DRV_GetChanResult(ADC_INSTANCE, 0U, &u16AdcRawValue);

    /* Process the result to get the value in volts */
    ftAdcValue = ((float) u16AdcRawValue / u16AdcMax) * (ADC_VREFH - ADC_VREFL);
    return ftAdcValue;
}

/********************************************************************************
    Description :
        Get Adc channel3 value
********************************************************************************/
PUBLIC FUNC(float,ADC_CODE)Adc_ftGetChannel3Value(void)
{
    uint16 u16AdcRawValue;
    float ftAdcValue;
    /* Configure ADC channel and software trigger a conversion */
    ADC_DRV_ConfigChan(ADC_INSTANCE, 0U, &ADC_CHANNEL_INDEX3);
    /* Wait for the conversion to be done */
    ADC_DRV_WaitConvDone(ADC_INSTANCE);
    /* Store the channel result into a local variable */
    ADC_DRV_GetChanResult(ADC_INSTANCE, 0U, &u16AdcRawValue);

    /* Process the result to get the value in volts */
    ftAdcValue = ((float) u16AdcRawValue / u16AdcMax) * (ADC_VREFH - ADC_VREFL);
    return ftAdcValue;
}

/********************************************************************************
    Private function :
********************************************************************************/

/********************************************************************************
    Description :
        The description of this function
********************************************************************************/
